package com.imau.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/responseServletStream")
public class ResponseServletStream extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.读取本地文件
        FileInputStream fis = new FileInputStream("E:/aaa.png");
        //2.获取输出流
        ServletOutputStream outputStream = resp.getOutputStream();
        //3.输出 边读取边输出
        byte buff[] = new byte[1024];
        int len = 0;

        while ((len = fis.read(buff)) != -1) {


            outputStream.write(buff, 0, len);

        }
        fis.close();
        outputStream.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
